package com.gy.hsim.bservice.framework;



import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.gy.hsim.bservice.asyncload.thread.AgingChatRecordThread;
import com.gy.hsim.bservice.asyncload.thread.ChatGroupRecordConsumerThread;
import com.gy.hsim.bservice.asyncload.thread.ChatPushRecordConsumerThread;
import com.gy.hsim.bservice.asyncload.thread.ChatRecordConsumerThread;
import com.gy.hsim.bservice.framework.bean.SystemConfig;
import com.gy.hsim.bservice.framework.mq.MQConnection;
import com.gy.hsim.bservice.framework.mq.MQMessageQueue;
import com.gy.hsim.bservice.framework.mq.MQParam;
import com.gy.hsim.bservice.framework.sensitive.SensitiveWordInit;
import com.gy.hsim.bservice.framework.spring.SpringUtil;
import com.gy.hsim.bservice.framework.spring.SystemApplicationContext;
import com.gy.hsim.bservice.friendInfo.service.FriendInfoService;
import com.rabbitmq.client.QueueingConsumer;

/**
 * 系统初始化类
 * 
 * @category 初始化类
 * @className SysInitBean
 * @description 用来启动聊天信息异步入库线程
 * @author Lee
 * @createDate 2015-01-08
 * @version v1.0
 */
@Component
public class SysInitor implements InitializingBean, ServletContextAware {

	// 配置信息
	@Autowired
	private SystemConfig config;
	
	
	@Autowired
	private FriendInfoService friendInfoService;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		
		/** 初始化敏感词数据*/
//		initSensitiveword();
		
		/** 执行老化聊天记录线程 */
		execAgingChartData();
		
		/** 执行写聊天记录日志数据线程 */
		writeChatRecordData();
		
		/** 执行写推送消息日志数据线程 */
		writeChatPushMsgRecordData();
		
		/** 执行写群消息日志数据线程 */
		writeChatGroupMsgRecordData();
			
	}
	
	private void initSensitiveword(){
		
		SystemApplicationContext systemApplicationContext = (SystemApplicationContext) SpringUtil.getBean("systemApplicationContext");
		
		systemApplicationContext.setSensitiveWordMap(new SensitiveWordInit().initKeyWord());
	}
	
	private void execAgingChartData() {
		AgingChatRecordThread thread = new AgingChatRecordThread(config.isNeedAgingChatRecordData(),config.getAgingChatRecordDays());
		thread.start();
	}
	
	private void writeChatRecordData() {
				
		        // 聊天信息线程池大小
				int chatRecordConsumerPoolSize = Integer.parseInt(config.getChatRecordConsumerPoolSize());
				
				// 创建指定大小的聊天消息入库线程池
				ExecutorService chatThreadConsumerPool = Executors
						.newFixedThreadPool(chatRecordConsumerPoolSize);
				
				QueueingConsumer chatRecordConsumer = null;
				
				try {
					/** 初始化聊天记录Consumer */
					chatRecordConsumer = MQConnection.getConsumerChannel(config.getMqUserName(),
							config.getMqPasswd(), config.getMqHostaddr(),
							config.getMqPort(), config.getChatrecordQueueName());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.exit(-1);
				}
				
				/** 根据线程池大小启动多个线程接收聊天记录报文  */
				for (int i = 0; i < chatRecordConsumerPoolSize; i++) {
					Thread t = new ChatRecordConsumerThread(friendInfoService,chatRecordConsumer, i,config);
					// 将线程放入池中进行执行
					chatThreadConsumerPool.execute(t);
				}
	}
			
	private void writeChatPushMsgRecordData() {
		
	        // 聊天信息线程池大小
			int chatRecordConsumerPoolSize = Integer.parseInt(config.getChatRecordConsumerPoolSize());
			
			// 创建指定大小的聊天消息入库线程池
			ExecutorService chatThreadConsumerPool = Executors
					.newFixedThreadPool(chatRecordConsumerPoolSize);
			
			QueueingConsumer chatRecordConsumer = null;
			
			try {
				/** 初始化聊天记录Consumer */
				chatRecordConsumer = MQConnection.getConsumerChannel(config.getMqUserName(),
						config.getMqPasswd(), config.getMqHostaddr(),
						config.getMqPort(), config.getChatPushRecordQueueName());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.exit(-1);
			}
			
			/** 根据线程池大小启动多个线程接收聊天记录报文  */
			for (int i = 0; i < chatRecordConsumerPoolSize; i++) {
				Thread t = new ChatPushRecordConsumerThread(friendInfoService,chatRecordConsumer, i,config);
				// 将线程放入池中进行执行
				chatThreadConsumerPool.execute(t);
			}
	}
	
	private void writeChatGroupMsgRecordData() {
		
	        // 聊天信息线程池大小
			int chatRecordConsumerPoolSize = Integer.parseInt(config.getChatRecordConsumerPoolSize());
			
			// 创建指定大小的聊天消息入库线程池
			ExecutorService chatThreadConsumerPool = Executors
					.newFixedThreadPool(chatRecordConsumerPoolSize);
			
			QueueingConsumer chatRecordConsumer = null;
			
			try {
				/** 初始化聊天记录Consumer */
				chatRecordConsumer = MQConnection.getConsumerChannel(config.getMqUserName(),
						config.getMqPasswd(), config.getMqHostaddr(),
						config.getMqPort(), config.getChatGroupRecordQueueName());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.exit(-1);
			}
			
			/** 根据线程池大小启动多个线程接收聊天记录报文  */
			for (int i = 0; i < chatRecordConsumerPoolSize; i++) {
				Thread t = new ChatGroupRecordConsumerThread(friendInfoService,chatRecordConsumer, i,config);
				// 将线程放入池中进行执行
				chatThreadConsumerPool.execute(t);
			}
	}
	
	@Override
	public void setServletContext(ServletContext arg0) {
	}

}
